import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class TestSort {
    //直接插入排序
    public static void insertSort(int[] array){
        for(int i=1;i<array.length;++i){
            int tmp = array[i];
            int j=i-1;
            for(;j>=0;--j){
                if(tmp<array[j]){
                    array[j+1]=array[j];
                }else{
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    //希尔排序
    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap>1){
            gap/=2;
            shell(array,gap);
        }
        shell(array,1);
    }
    public static void shell(int[] array,int gap){
        for(int i=gap;i<array.length;++i){
            int tmp=array[i];
            int j=i-gap;
            for(;j>=0;j-=gap){
                if(tmp<array[j]){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }
    //直接选择排序
    public static void selectSort(int[] array){
        for(int i=0;i<array.length;++i){
            int minIndex = i;
            for(int j=i+1;j<array.length;++j){
                if(array[j]<array[minIndex]){
                    minIndex=j;
                }
            }
            swap(array,minIndex,i);
        }
    }
    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    //堆排序
    public static void heapSort(int[] array){

    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        for(int i=0;i<array.length-1;++i){
            boolean flag=false;
            for(int j=0;j<array.length-1-i;++j){
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flag=true;
                }
            }
            if(flag==false){
                break;
            }
        }
    }
    //快速排序
    public static void quickSort(int[] array){
        quick(array,0,array.length-1);
    }
    public static void quick2(int[] array,int start,int end){
        if(start>=end){
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    //挖坑法
    public static int partition(int[] array,int left,int right){
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    //hoare法
    public static int partition2(int[] arrays, int left, int right) {
        int tmp = arrays[left];
        int oldLeft = left;
        while (left < right) {
            while (left < right && arrays[right] >= tmp) {
                right--;
            }
            while (left < right && arrays[left] <= tmp) {
                left++;
            }
            if (left < right) {
                swap(arrays, left, right);
            }
        }
        if (left == right) {
            arrays[oldLeft] = arrays[left];
            arrays[left] = tmp;
        }
        return left;
    }
    //前后指针法
    public static int partition3(int arr[], int left, int right) {
        int pCur = left;
        int prev = pCur - 1;
        int key = arr[right];
        while (pCur <= right) {
            if (arr[pCur] <= key && (++prev) != pCur)
                swap(arr, prev, pCur);
            pCur++;
        }
        return prev;
    }
    //三数取中
    private static int threeMid(int[] array,int left,int right) {
        int mid = (left+right) >>> 1;
        if(array[left] < array[right]) {
            if(array[mid] < array[left]) {
                return left;
            }else if(array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            // array[left] > array[right]
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }
    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }

        if(end-start+1 <= Constant.INSERT_SIZE) {
            //插入排序
            insertSort2(array,start,end);
            return;
        }
        int index = threeMid(array,start,end);
        swap(array,index, start);
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    private static void insertSort2(int[] array,int start,int end) {

        for (int i = start+1 ; i <= end; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= start; j--) {
                //>=
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;//
                }
            }
            array[j+1] = tmp;
        }
    }
    public static void quickSort2(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int start = 0;
        int end = array.length-1;
        int pivot = partition(array,start,end);
        //左边有2个元素及以上
        if(pivot > start+1) {
            stack.push(start);
            stack.push(pivot-1);
        }
        //右边有2个元素及以上
        if(pivot < end-1) {
            stack.push(pivot+1);
            stack.push(end);
        }
        while (!stack.empty()) {
            end = stack.pop();
            start = stack.pop();
            pivot = partition(array,start,end);
            //左边有2个元素及以上
            if(pivot > start+1) {
                stack.push(start);
                stack.push(pivot-1);
            }
            //右边有2个元素及以上
            if(pivot < end-1) {
                stack.push(pivot+1);
                stack.push(end);
            }
        }
    }
    //归并排序
    public static void mergeSort(int[] array) {
        mergeSortFunction(array,0,array.length-1);
    }
    private static void mergeSortFunction(int[] array,int low,int high) {
        if(low >= high) {
            return;
        }
        int mid = (low+high) >>> 1;
        mergeSortFunction(array,low,mid);
        mergeSortFunction(array,mid+1,high);
        merge(array,low,high,mid);
    }
    private static void merge(int[] array,int low,int high,int mid) {
        int[] tmp = new int[high-low+1];
        int k = 0;
        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;
        while (s1 <= e1 && s2 <= e2) {
            //两个归并段  都有数据
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+low] = tmp[i];
        }
    }
    public static void mergeSort2(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += gap*2 ) {
                int low = i;
                int mid = low+gap-1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int high = mid+gap;
                if(high >= array.length) {
                    high = array.length-1;
                }
                merge(array,low,high,mid);
            }
            gap = gap*2;
        }
    }
    public static void main(String[] args) {
        int[] array = {2,3,9,6,8,7,4,5};
        mergeSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main2(String[] args) {
        int[] array = new int[10_0000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
            //array[i] = random.nextInt(10_0000);
        }
        long startTime = System.currentTimeMillis();
        quickSort(array);
        long endTimes = System.currentTimeMillis();
        System.out.println(endTimes-startTime);
    }
}
